package com.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;

import com.entity.view.GongwenfenleiView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;

import com.entity.GongwenfenleiEntity;

import com.service.GongwenfenleiService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;

/**
 * 公文分类
 * 后端接口
 * @author 
 * @email 
 * @date 2023-03-05 15:45:54
 */
@RestController
@RequestMapping("/gongwenfenlei")
public class GongwenfenleiController {
    @Autowired
    private GongwenfenleiService gongwenfenleiService;

    /**
     * 后端列表接口
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params,GongwenfenleiEntity gongwenfenlei,
		HttpServletRequest request){
        EntityWrapper<GongwenfenleiEntity> ew = new EntityWrapper<GongwenfenleiEntity>();

		PageUtils page = gongwenfenleiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, gongwenfenlei), params), params));

        return R.ok().put("data", page);
    }

	@RequestMapping("/gongwenfenlei")
	public R gongwenfenlei(){
		EntityWrapper<GongwenfenleiEntity> ew = new EntityWrapper<GongwenfenleiEntity>();

		List<GongwenfenleiView> gongwenfenleiViews = gongwenfenleiService.selectListView(ew);
		List<String> collect = gongwenfenleiViews.stream().map(GongwenfenleiView::getGongwenfenlei).collect(Collectors.toList());
		return R.ok().put("data", collect);
	}

    /**
     * 后端详情接口
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        GongwenfenleiEntity gongwenfenlei = gongwenfenleiService.selectById(id);
        return R.ok().put("data", gongwenfenlei);
    }

    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GongwenfenleiEntity gongwenfenlei, HttpServletRequest request){
    	gongwenfenlei.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
    	//ValidatorUtils.validateEntity(gongwenfenlei);
        gongwenfenleiService.insert(gongwenfenlei);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody GongwenfenleiEntity gongwenfenlei, HttpServletRequest request){
        //ValidatorUtils.validateEntity(gongwenfenlei);
        gongwenfenleiService.updateById(gongwenfenlei);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        gongwenfenleiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
    
    /**
     * 提醒接口
     */
	@RequestMapping("/remind/{columnName}/{type}")
	public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request, 
						 @PathVariable("type") String type,@RequestParam Map<String, Object> map) {
		map.put("column", columnName);
		map.put("type", type);
		
		if(type.equals("2")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			Date remindStartDate = null;
			Date remindEndDate = null;
			if(map.get("remindstart")!=null) {
				Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
				c.setTime(new Date()); 
				c.add(Calendar.DAY_OF_MONTH,remindStart);
				remindStartDate = c.getTime();
				map.put("remindstart", sdf.format(remindStartDate));
			}
			if(map.get("remindend")!=null) {
				Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
				c.setTime(new Date());
				c.add(Calendar.DAY_OF_MONTH,remindEnd);
				remindEndDate = c.getTime();
				map.put("remindend", sdf.format(remindEndDate));
			}
		}
		
		Wrapper<GongwenfenleiEntity> wrapper = new EntityWrapper<GongwenfenleiEntity>();
		if(map.get("remindstart")!=null) {
			wrapper.ge(columnName, map.get("remindstart"));
		}
		if(map.get("remindend")!=null) {
			wrapper.le(columnName, map.get("remindend"));
		}
		int count = gongwenfenleiService.selectCount(wrapper);
		return R.ok().put("count", count);
	}

}
